﻿using System;
using BalanceManager.Helpers;
using BalanceManager.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using BalanceManager.Services;

namespace BalanceManager.ViewModels
{
    public class MainViewModel : BaseViewModel, INavigable
    {
        readonly DelegateCommand _addExpenseCommand;
        readonly DelegateCommand _editFundFlowCommand;
        readonly DelegateCommand _addProfitCommand;
        readonly DelegateCommand _deleteFundFlowCommand;
        readonly DelegateCommand _itemsCommand;
        readonly DelegateCommand _historyCommand;

        readonly private IDataService dataService;
        private ObservableCollection<FundFlow> fundFlows;

        public MainViewModel(IDataService dataService)
        {
            if (dataService == null)
                throw new ArgumentException("dataService");

            this.dataService = dataService;

            _addExpenseCommand = new DelegateCommand(AddExpenseAction);
            _editFundFlowCommand = new DelegateCommand(EditFundFlowAction);
            _addProfitCommand = new DelegateCommand(AddProfitAction);
            _deleteFundFlowCommand = new DelegateCommand(DeleteFundFlowAction);
            _itemsCommand = new DelegateCommand(ItemsAction);
            _historyCommand = new DelegateCommand(HistoryAction);
        }

        public ObservableCollection<FundFlow> FundFlows
        {
            get { return fundFlows; }
            set
            {
                if (fundFlows == value) return;
                fundFlows = value;
                OnPropertyChanged();
            }
        }
        
        public DelegateCommand AddExpenseCommand
        {
            get { return _addExpenseCommand; }
        }

        public DelegateCommand EditFundFlowCommand
        {
            get { return _editFundFlowCommand; }
        }

        public DelegateCommand AddProfitCommand
        {
            get { return _addProfitCommand; }
        }

        public DelegateCommand DeleteFundFLowCommand
        {
            get { return _deleteFundFlowCommand; }
        }

        public DelegateCommand ItemsCommand
        {
            get { return _itemsCommand; }
        }

        public DelegateCommand HistoryCommand
        {
            get { return _historyCommand; }
        }

        void AddExpenseAction(object o)
        {
            var fundFlow = new FundFlow(FlowType.Expense);
            var param = new FundFlowEditableParameter() { EditingMode = false, FundFlow = fundFlow };
            ApplicationController.Default.NavigationParameters.Add("fundFlowParam", param);
            ApplicationController.Default.NavigateTo(ViewType.AddFundFlow);
        }

        void AddProfitAction(object o)
        {
            var fundFlow = new FundFlow(FlowType.Profit);
            var param = new FundFlowEditableParameter() {EditingMode = false, FundFlow = fundFlow};
            ApplicationController.Default.NavigationParameters.Add("fundFlowParam", param);
            ApplicationController.Default.NavigateTo(ViewType.AddFundFlow);
        }

        void EditFundFlowAction(object o)
        {
            var param = new FundFlowEditableParameter() {EditingMode = true, FundFlow = o as FundFlow};
            ApplicationController.Default.NavigationParameters.Add("fundFlowParam", param);
            ApplicationController.Default.NavigateTo(ViewType.AddFundFlow);
        }

        void DeleteFundFlowAction(object o)
        {
            var fundFlow = (FundFlow) o;
            dataService.DeleteFundFlow(fundFlow.Id);
            ReloadFundFlows();
        }

        void ItemsAction(object o)
        {
            ApplicationController.Default.NavigateTo(ViewType.Items);
        }

        void HistoryAction(object o)
        {
            ApplicationController.Default.NavigateTo(ViewType.History);
        }

        void ReloadFundFlows()
        {
            var now = DateTime.Now;
            var dateFrom = new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);
            var dateTo = new DateTime(now.Year, now.Month, now.Day, 23, 59, 59);
            FundFlows = new ObservableCollection<FundFlow>(dataService.LoadFundFlows(dateFrom, dateTo));
        }

        public void Activate(IDictionary<string, object> parameters)
        {
            ReloadFundFlows();
        }

        public void Deactivate(IDictionary<string, object> parameters)
        {

        }
    }
}